#ifndef MEDIAWIDGET_H
#define MEDIAWIDGET_H

#include "lyriclabel.h"
#include "musictableview.h"
#include "tablemodel.h"

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QSlider>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QTableWidget>
#include <QSystemTrayIcon>
QT_BEGIN_NAMESPACE
namespace Ui { class MediaWidget; }
QT_END_NAMESPACE

class MediaWidget : public QWidget
{
    Q_OBJECT

public:
    MediaWidget(QWidget *parent = nullptr);
    ~MediaWidget();
    void initMediaPlayer();
    void playClickedMedia(const QModelIndex &index);
    void openMedia();
    void openMediaByName(QString fileName);
    void loadLyric(QString fileName);
    void process_line(QString line);
    void fastForward();
    void fastBackward();
    void controlMedia(bool isPlay);
    void resetMedia();
    void silent();
    void changeVolumn(int vol);
    void mapSliderToPosition(float val);
    void changeProcess(int val);
    void processChanged(int val);
    void updateLyric(int curMsec);
    void updateProcessText(int val);
    void setReady(QMediaPlayer::MediaStatus status);
    void updateWidget(QMediaPlayer::PlaybackState state);
    void showMusicListWidget();
    void quit();
    protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
    void closeEvent(QCloseEvent *event) override;
    void writeSettings();
signals:
    void musicMapChanged(QString fileName, QMap<QString, QString> *musicMsg);
private:
    Ui::MediaWidget *ui;
    QMediaPlayer *player;
    QAudioOutput *output;
    bool isReady = false;
    QMap<QString, QMap<QString, QString> *> *musicMsgMap;
    QMap<QString, QString> *musicMsg;
    QMap<int, QString> lyric;
    int curLyricStartTime = 0;
    bool wasPlaying;
    TableModel *tableModel;
    MusicTableView *tableWidget = nullptr;
    TableModel::PlayType curType = TableModel::ROUND;
    QModelIndex curIndex;
    LyricLabel *lyricLabel;
    QSystemTrayIcon *trayIcon;
private slots:
    void activateTrayIcon(QSystemTrayIcon::ActivationReason reason);
};
#endif // MEDIAWIDGET_H
